package com.example.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.eduservice.entity.EduChapter;
import com.example.eduservice.entity.EduVideo;
import com.example.eduservice.entity.chapter.ChapterVo;
import com.example.eduservice.entity.chapter.VideoVo;
import com.example.eduservice.mapper.EduChapterMapper;
import com.example.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.eduservice.service.EduVideoService;
import com.example.handler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author xiuxian
 * @since 2022-06-26
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService  videoService;

    //课程大纲列表 根据id查询
    @Override
    public List<ChapterVo> getChapterVideoByid(String courseId) {
        //根据课程id查询所有章节
        QueryWrapper<EduChapter> chapterWrapper=new QueryWrapper<>();
        chapterWrapper.eq("course_id",courseId);
        List<EduChapter> chapterList = baseMapper.selectList(chapterWrapper);


        //根据课程id查询所有小节
        QueryWrapper<EduVideo> videoWrapper=new QueryWrapper<>();
        videoWrapper.eq("course_id",courseId);
        List<EduVideo> videoList = videoService.list(videoWrapper);

        //创建一个list集合返回最终数据
        List<ChapterVo> finalList=new ArrayList<>();

        //遍历章节list集合进行封装

        for (int i = 0; i < chapterList.size(); i++) {

            //创建章节对象
            EduChapter eduChapter=chapterList.get(i);


            ChapterVo chapterVo=new ChapterVo();

            BeanUtils.copyProperties(eduChapter,chapterVo);

            //创建小节list集合
            List<VideoVo> videoVoList=new ArrayList<>();

            //遍历小节list集合进行封装
            for (int m = 0; m < videoList.size(); m++) {


                EduVideo eduVideo=videoList.get(m);
                //判断小节的章节id是否和查出来的一样
                if(eduVideo.getChapterId().equals(eduChapter.getId())){

                    VideoVo videoVo=new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    //添加到小节列表
                    videoVoList.add(videoVo);
                }

            }

            //设置章节包含的小节
            chapterVo.setChildren(videoVoList);
            //将包含章节的对象返回
            finalList.add(chapterVo);

        }





        return finalList;
    }

    //根据id删除章节
    @Override
    public boolean deleteChapter(String chapterId) {
        //如果要删除的章节有小节则不可以删除
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = videoService.count(wrapper);
        //判断是否有小节数据
        if(count>0){
            throw  new GuliException(20001,"删除失败");
        }else{
            //删除章节
            int result = baseMapper.deleteById(chapterId);

            //成功 1》0  失败 0》0
                return  result>0;

        }
    }

    @Override
    public void removeChapterByid(String courseId) {
        //构造条件
        QueryWrapper<EduChapter> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
